651A - Joysticks - CodeForces Solution


dp greedy implementation math *1100

Please click on ads to support us..

Python Code:

import math
a, b = map(int, input().split())
ans = 0
while a > 0 and b > 0:
    ans += 1
    if a >= b:
        a -= 2
        b += 1
    else:
        a += 1
        b -= 2
if a < 0 or b < 0:
    ans -= 1
print(ans)

C++ Code:

#include<bits/stdc++.h>
#define ORIGNAL_SENSEI ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD=1e9+7, OO=0x3f3f3f3f;
const ll LOO=0x3f3f3f3f3f3f3f3f;
#define all(x) (x).begin(),(x).end()
#define mm(f, x) memset(f,x,sizeof(f))
#define f(n) for(int i=0;i<n;++i)
#define fa(i,a,n) for(int i=(a);i<=(n);++i)
#define  c(vec) for(auto &x:vec)cin>>x;
#define ff(vec) for(auto &x:vec)cout<<x<<' ';cout<<'\n';
#define debug(x) cout<<#x<<":"<<x<<endl;
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
//#define f(n,m) for(int i=0;i<n;i++)for(int j=0;j<m;j++)
const long double EPS=1e-8;
const ll MOD1=1e18+7;
int dr[]={-1, -1, -1, 0, 1, 1,  1,  0};
int dc[]={-1,  0,  1, 1, 1, 0, -1, -1};
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};



int main() {
    ORIGNAL_SENSEI
    //freopen("input.txt","rt",stdin);
    //freopen("output.txt","wt",stdout);
    int n,m;
    cin>>n>>m;
    int mn=min(n,m);
    int mx=max(n,m);
    ll ans=0;
    bool flag=0;
    while(mx>1||mn>1){
        if(mx-2>=0){
            mx-=2;
            if(mx==0)flag=1;
            mn++;
            ans++;
        }
        else {
            swap(mn,mx);
        }

    }
    if(flag)ans--;
    cout<<ans<<'\n';

}


Comments

Submit
0 Comments
More Questions

347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays